package com.dupf.product.rpc.service.impl;

import com.dupf.common.annotation.BaseService;
import com.dupf.common.base.BaseServiceImpl;
import com.dupf.common.util.ItvJsonUtil;
import com.dupf.common.util.RedisUtil;
import com.dupf.product.common.constant.ProductConstant;
import com.dupf.product.dao.mapper.ProductVariantMapper;
import com.dupf.product.dao.model.ProductVariant;
import com.dupf.product.dao.model.ProductVariantExample;
import com.dupf.product.rpc.api.ProductVariantService;
import net.sf.json.JSONArray;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
* ProductVariantService实现
* Created by dupf on 2018/10/18.
*/
@Service
@Transactional
@BaseService
public class ProductVariantServiceImpl extends BaseServiceImpl<ProductVariantMapper, ProductVariant, ProductVariantExample> implements ProductVariantService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ProductVariantServiceImpl.class);

    @Autowired
    ProductVariantMapper productVariantMapper;

    @Override
    public Integer getOrder(ProductVariant productVariant) {
        Integer order = 0;
        if(StringUtils.isNotEmpty(productVariant.getVariantZh())){
            order++;
        }
        if(StringUtils.isNotEmpty(productVariant.getVariantEn())){
            order++;
        }
        if(StringUtils.isNotEmpty(productVariant.getVariantFr())){
            order++;
        }
        if(StringUtils.isNotEmpty(productVariant.getVariantDe())){
            order++;
        }
        if(StringUtils.isNotEmpty(productVariant.getVariantEs())){
            order++;
        }
        if(StringUtils.isNotEmpty(productVariant.getVariantJa())){
            order++;
        }
        if(StringUtils.isNotEmpty(productVariant.getVariantPt())){
            order++;
        }
        if(StringUtils.isNotEmpty(productVariant.getVariantIt())){
            order++;
        }
        return order;
    }

    @Override
    public Integer addVariant(ProductVariant productVariant) {
        return productVariantMapper.insertSelective(productVariant);
    }

    @Override
    public List<Map<String, Object>> getUserVariantCountList(Integer orgId) {

        // 缓存中获取
        String redisResult = RedisUtil.get(ProductConstant.VARIANT_ORG_USER_LIST_REDIS_PREFIX + orgId);

        if (StringUtils.isEmpty(redisResult)) {

            List<Map<String, Object>> result = productVariantMapper.selectUserIdsByOrgIdGroupByUserId(orgId);
            JSONArray resultArray = JSONArray.fromObject(result);

            RedisUtil.set(ProductConstant.VARIANT_ORG_USER_LIST_REDIS_PREFIX + orgId, resultArray.toString(), ProductConstant.VARIANT_ORG_USER_LIST_REDIS_EXPIRE * 60);
            return result;
        } else {
            return JSONArray.fromObject(redisResult);
        }
    }
}